package com.gomcarter.frameworks.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gomcarter.frameworks.base.aop.CrossAccessFilter;
import com.gomcarter.frameworks.base.spring.CustomMappingJacksonConverter;
import com.gomcarter.frameworks.base.spring.SpringContextHolder;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.unit.DataSize;
import org.springframework.web.filter.FormContentFilter;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.MultipartConfigElement;
import java.nio.charset.Charset;
import java.util.Iterator;
import java.util.List;

/**
 * 过滤器配置
 *
 * @author gomcarter on 2018年3月29日 16:29:34
 */
@Configuration
public class CommonConfiguration implements WebMvcConfigurer {

    /**
     * 设置允许可以跨域访问
     *
     * @return CrossAccessFilter
     */
    @Bean
    @ConditionalOnMissingBean
    public CrossAccessFilter crossAccessFilter() {
        return new CrossAccessFilter();
    }

    /**
     * @return FormContentFilter
     */
    @Bean
    @ConditionalOnMissingBean
    public FormContentFilter httpPutFormContentFilter() {
        return new FormContentFilter();
    }

    /**
     * @return SpringContextHolder
     */
    @Bean
    public SpringContextHolder springContextHolder() {
        return new SpringContextHolder();
    }


    private CustomMappingJacksonConverter customMappingJacksonConverter = new CustomMappingJacksonConverter();

    /**
     * @return jacksonObjectMapper
     */
    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper objectMapper() {
        return customMappingJacksonConverter.getObjectMapper();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
        while (iterator.hasNext()) {
            HttpMessageConverter<?> messageConverter = iterator.next();
            if (messageConverter instanceof StringHttpMessageConverter) {
                Charset charset = ((StringHttpMessageConverter) messageConverter).getDefaultCharset();
                if (!Charset.defaultCharset().equals(charset)) {
                    iterator.remove();
                }
            }
            if (messageConverter instanceof MappingJackson2HttpMessageConverter) {
                iterator.remove();
            }
        }

        converters.add(0, customMappingJacksonConverter);
    }

    /**
     * 文件上传配置
     */
    @Bean
    @ConditionalOnMissingBean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 单个文件最大 --- 1024 M
        factory.setMaxFileSize(DataSize.ofKilobytes(1024 * 1000));
        // 设置总上传数据总大小 --- 3072 M
        factory.setMaxRequestSize(DataSize.ofKilobytes(3 * 1024 * 1000));
        return factory.createMultipartConfig();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
    }
}
